#!/bin/bash

echo "开始时间 $(date +"%Y-%m-%d %H:%M:%S")"

running_path=$(pwd)
echo "运行脚本的目录: ${running_path}"

# 获取配置文件
project_info_json_string=$(cat config/project-info.json) || echo "获取项目配置文件失败，请在脚本同目录下创建：config/project-info.json"

# 处理项目路径
project_location=$(echo "${project_info_json_string}" | jq -r '.project_location') || project_location=""
if [ "null" == "${project_location}" ] || [ -z "${project_location}" ]; then
  cd ..
  project_location=$(pwd)
  echo "使用默认配置文件: $project_location"
elif [ -d "${project_location}" ]; then
  echo "配置文件中的项目路径：$project_location"
else
  echo "配置文件中的项目路径：${project_location}不存在"
  cd ..
  project_location=$(pwd)
  echo "使用默认路径: $project_location"
fi

# 处理命令行输入选项
SERVICE_NAME="" # 服务的名称，模糊匹配获取完整名称
ENV=""          # 开发环境
OUTPUT_DIR=""
INVOKE_TEMP=""
while getopts ":n:e:o:t:" opt; do
  case $opt in
  n)
    SERVICE_NAME=$OPTARG
    ;;
  e)
    ENV=$OPTARG
    ;;
  o)
    OUTPUT_DIR=$OPTARG
    ;;
  t)
    INVOKE_TEMP=$OPTARG
    ;;
  ?)
    echo "可用选项：-n 服务名称 -e 开发环境 -o jar包输出目录（默认输出到项目下的output目录）"
    exit 1
    ;;
  esac
done

if [ -z "${SERVICE_NAME}" ]; then
  echo "未指定服务项目名称 -n"
  exit
fi

if [ -z "${ENV}" ]; then
  echo "未指定开发环境 -e"
  exit
fi

# 从配置文件中获取服务的完整名称
match_service_info_json_array=()
service_info_length=$(echo "${project_info_json_string}" | jq -r '.service_info | length')
#echo "配置文件有${service_info_length}个服务配置"
# 遍历配置文件
for ((i = 0; i < "${service_info_length}"; i++)); do
  service_info_json=$(echo "${project_info_json_string}" | jq -r ".service_info[${i}]")
  model_name=$(echo "${service_info_json}" | jq -r .model_name)
  if [[ ${model_name} =~ ${SERVICE_NAME} ]]; then
    match_service_info_json_array[${#match_service_info_json_array[@]}]=${service_info_json}
    break
  fi
done
# 服务名称的匹配值
if [ ${#match_service_info_json_array[@]} -eq 0 ]; then
  echo "输入的服务名称:${SERVICE_NAME}在配置文件中找不到匹配项目"
  exit 1
fi
match_service_info_json=${match_service_info_json_array[0]}
echo "服务名称的匹配值：$match_service_info_json"

# 校验输入的开发环境值是否正确
env_info=$(echo "${project_info_json_string}" | jq -r '.env_info') || env_info=""
if [ -n "${env_info}" ]; then
  if [[ ! ("${env_info}" =~ ${ENV}) ]]; then
    echo "开发环境${ENV}错误，取值范围：${env_info}"
    exit 1
  fi
fi

# 从远程仓库中更新同步代码
echo "当前代码版本: $(git log -1 --pretty=format:'%ci %cN %s')"
echo '拉取最新的代码...'
eval "git pull"

# 清理旧文件
jar_name=$(echo "${match_service_info_json}" | jq -r .jar_name)
jar_name="output/${jar_name}.jar"
if [ -f "${jar_name}" ]; then
  jar_info=$(stat "${jar_name}" -c '%n %s %w')
  echo "旧文件：${jar_info}"
  echo "清理旧文件"
  rm "${jar_name}"
fi

# 使用maven构建打包指定项目
model_name=$(echo "${match_service_info_json}" | jq -r .model_name)
parent_model=$(echo "${match_service_info_json}" | jq -r .parent_model)
maven_package_script="mvn clean -DskipTests package -pl ${parent_model}/${model_name} -am -amd -P ${ENV}"
echo "执行maven打包命令: ${maven_package_script}"
package_result=$(${maven_package_script})
#echo "${package_result}"
#eval "${maven_package_script}"

# 查看新文件
jar_info=$(stat "${jar_name}" -c '%n %s %w')
full_jar_path=$(readlink -f "${jar_name}")

if [ -n "${INVOKE_TEMP}" ]; then
  echo "${full_jar_path}" > "script/${INVOKE_TEMP}"
fi

# 将新建复制到指定目录
if [[ -n "${OUTPUT_DIR}" ]]; then
  if [[ -d "${OUTPUT_DIR}" ]]; then
    echo "将新文件复制到目录: ${OUTPUT_DIR}"
    cp -p "${jar_name}" "$OUTPUT_DIR"
  else
    echo "目录${OUTPUT_DIR}不存在"
  fi
fi
